<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleAudit" v-if="hasPermission('xl:xl_customer_aftersale:audit1')">
                <Icon icon="ant-design:edit-outlined"></Icon>
                审核
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'xl:xl_customer_aftersale:audit'">批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <template #asCodeRender="{ text, record }">
        <div>单号：{{ record.asCode }}</div>
        类型：<a-tag v-if="record.asType == '仅退款-整单退'" color='#39CCCC'>{{ record.asType }}</a-tag>
        <a-tag v-if="record.asType == '仅退款-部分退'" color='#7FDBFF'>{{ record.asType }}</a-tag>
        <a-tag v-if="record.asType == '退货退款-整单退'" color='#F012BE'>{{ record.asType }}</a-tag>
        <a-tag v-if="record.asType == '退货退款-部分退'" color='#3D9970'>{{ record.asType }}</a-tag>
        <div>状态：<a-tag v-if="record.asStatus == '待审核'" color='#FF4136'>{{ record.asStatus }}</a-tag>
          <a-tag v-if="record.asStatus == '退款中'" color='#FF851B'>{{ record.asStatus }}</a-tag>
          <a-tag v-if="record.asStatus == '退款成功'" color='#2ECC40'>{{ record.asStatus }}</a-tag>
          <a-tag v-if="record.asStatus == '拒绝'" color='#AAAAAA'>{{ record.asStatus }}</a-tag>
        </div>
      </template>



      <template #orderInfoRender="{ text, record }">
        <div>订单：{{ record.orderCode }}</div>
        <div>门店：{{ record.storeName }}</div>
      </template>

      <template #asDataRender="{ text, record }">
        <div>售后商品总数量/件：{{ record.productTotalQuantity }}</div>
        <div>售后商品种数/种：{{ record.productCategoryCount }}</div>
        <div>申请金额/元：{{ record.productTotalQuantity }} </div>
        <div>退款金额/元：{{ record.productTotalQuantity }} </div>
      </template>

    </BasicTable>
    <!-- 表单区域 -->

    <AuditModal @register="submitAuditModal" :schemas="auditFormSchema" :apiUrl="audit" :height="150"
      @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="xl-xlCustomerAftersale" setup>
import { ref, reactive, computed, unref } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { useModal } from '/@/components/Modal';
import AftersaleDetailModel from './components/AftersaleDetailModel.vue'
import AftersaleAddModal from './components/AftersaleAddModal.vue'
import { columns, searchFormSchema,auditFormSchema } from './as.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, audit } from './as.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { useUserStore } from '/@/store/modules/user';
import AllModal from '/@/views/xl/customer_orde/components/AllModal.vue'
import { useMessage } from "/@/hooks/web/useMessage";
const queryParam = reactive<any>({});
const checkedKeys = ref<Array<string | number>>([]);
const userStore = useUserStore();
import { usePermission } from '/@/hooks/web/usePermission';
const { hasPermission } = usePermission();
const { createConfirm, createMessage } = useMessage();
import { Image } from 'ant-design-vue';
import AuditModal from './AuditModal.vue';
const props = defineProps({
  pageType: { type: String, default: '0' },
});

//页面类型读取
const getProps = (val?) => {
  if (!!val) {
    return props.pageType === val;
  }
  return props.pageType === '0';
}


//注册model
const [registerEditModal, registerEditUseModal] = useModal();
const [registerModal, { openModal }] = useModal();
const [submitAuditModal, submitAuditUseModal] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls } = useListPage({
  tableProps: {
    title: '售后单',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    actionColumn: {
      width: 80,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      params.pageType = props.pageType; //带上页面类型参数
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "售后单",
    url: getExportUrl,
    params: queryParam,
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext


/**
 * 编辑
*/
function handleEdit(record: Recordable) {
  registerEditUseModal.openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}

/**
 * 详情
*/
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}


/**
 * 批量审核确认
 */
async function batchHandleAudit() {
  let isFLa = true
  selectedRows.value?.forEach(item => {
    if (item.woStatus != "0") {
      isFLa = false
    }
  })
  if (isFLa) {
    submitAuditUseModal.openModal(true, {
      title: '批量售后审核',
      record: { id: selectedRowKeys.value?.join(',') },
    });

  } else {
    createMessage.warning('不是待审批的不能操作！')
  }
}

/**
 * 审核确认
 */
async function handleAudit(record) {
  // await audit({ id: record.id });
  // handleSuccess()
  submitAuditUseModal.openModal(true, {
    title: '售后审批',
    record,
  });
}


/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 操作栏
   */
function getTableAction(record) {
  return [

    {
      label: '审核',
      onClick: handleAudit.bind(null, record),
      ifShow: record.asStatus === '待审核',
    }
  ]
}


/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
  ]
}

</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}
</style>